package com.alinesno.cloud.portal.desktop.web.pages;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.base.boot.entity.ManagerSettingsEntity;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.facade.response.ResponseGenerator;
import com.alinesno.cloud.common.web.base.controller.BaseController;
import com.alinesno.cloud.common.web.base.utils.ManagerSettingsUtils;
import com.alinesno.cloud.portal.desktop.web.bean.MenusBean;
import com.alinesno.cloud.portal.desktop.web.entity.LinkPathEntity;
import com.alinesno.cloud.portal.desktop.web.entity.ModuleEntity;
import com.alinesno.cloud.portal.desktop.web.repository.LinkPathRepository;
import com.alinesno.cloud.portal.desktop.web.service.IMenusService;
import com.alinesno.cloud.portal.desktop.web.service.IModuleService;

/**
 * 控制层
 * @author WeiXiaoJin
 * @since 2018年11月27日 上午6:41:40
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
public class PortalController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(PortalController.class) ; 

	@Autowired
	private IMenusService menusService ; 
	
	@Autowired
	private LinkPathRepository linkPathRepository ; 
	
	@Autowired
	private IModuleService moduleService ; 
	
	@Autowired
	private ManagerSettingsUtils managerSettingsUtils ; 

	/**
	 * 首页
	 * @param model
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/public/portal")
	public String portal(Model model , String type ,  HttpServletRequest request) {
		log.debug("进入首页");
		
//		// 模块
//		List<ModuleEntity> ms = moduleService.findAllModule() ; 
//		request.setAttribute("ms", ms); 
//		
//		// 常用功能
//		List<LinkPathEntity> fs = linkPathRepository.findAll() ; 
//		request.setAttribute("fs", fs); 
	
		// TODO 待排查不生效原因
		ManagerSettingsEntity redirect = managerSettingsUtils.queryKey("cloud.portal.redirect", null) ; //.getSettings().find
		if(redirect != null) {
			return this.redirect(redirect.getConfigValue()) ; 
		}

		return product(model , type , request) ; //默认显示产品页
	}
	
	/**
	 * 产品页
	 * @param model
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/public/product")
	public String product(Model model , String type ,  HttpServletRequest request) {
		log.debug("进入首页");
		
		// 模块
		List<ModuleEntity> ms = moduleService.findAllModule() ; 
		request.setAttribute("ms", ms); 
		
		// 常用功能
		List<LinkPathEntity> fs = linkPathRepository.findAll() ; 
		request.setAttribute("fs", fs); 

		return "portal/views/index" ; 
	}

	/**
	 * 显示所有菜单 
	 * @param model
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/public/navigation")
	public ResponseBean navigation(Model model , HttpServletRequest request) {
		log.debug("进入首页");
	
		List<MenusBean> menus = menusService.findNavigation() ; 
		model.addAttribute("menus", menus) ; 
		
		for(MenusBean b : menus) {
			log.debug("menusBean:{}" , ToStringBuilder.reflectionToString(b));
		}
		
		return ResponseGenerator.genSuccessResult(menus) ; 
	}

	/**
	 * 显示所有链接
	 * @param model
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/public/links")
	public ResponseBean links(Model model , String type , HttpServletRequest request) {
		log.debug("链接查询,类型:{}" , type);
	
		List<LinkPathEntity> menus = linkPathRepository.findAllByLinkDesign(type) ; 
		model.addAttribute("menus", menus) ; 
		
		for(LinkPathEntity b : menus) {
			log.debug("LinkPathEntity:{}" , ToStringBuilder.reflectionToString(b));
		}
		
		return ResponseGenerator.genSuccessResult(menus) ; 
	}

	/**
	 * 显示架构
	 * @param model
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/public/architecture")
	public String architecture(Model model , String type ,  HttpServletRequest request) {
		log.debug("进入架构显示");

		return "portal/views/architecture" ; 
	}
	
}
